package com.hibikiluler.javalearning.mapper;

import com.hibikiluler.javalearning.entity.Book;
import com.hibikiluler.javalearning.entity.BookCollect;
import com.hibikiluler.javalearning.entity.Learn;
import com.hibikiluler.javalearning.entity.LearnCollect;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ItemCollectMapper {
    /**
     *  查找操作——查找收藏资讯
     * @param userId
     * @return
     */
    @Select("select learning_information.id,title,create_name,collect_time from learning_information " +
            "left join user_learn on learning_information.id = user_learn.learn_id\n" +
            "                where learning_information.id in\n" +
            "                                   (select learn_id from user_learn where user_id=#{userId})")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "title",property = "title"),
            @Result(column = "collect_time",property = "collectTime"),
            @Result(column = "create_name",property = "createName")
    })
    List<LearnCollect> findCollectLearn(@Param("userId") Long userId);

    /**
     * 查找操作——查找收藏书籍
     * @param userId
     * @return
     */
    @Select("select book.id,title,image_urls,copyright,writer,collect_time from book " +
            "left join user_book on book.id = user_book.book_id\n" +
            "                where book.id in\n" +
            "                                   (select book_id from user_learn where user_id=#{userId})")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "title",property = "title"),
            @Result(column = "collect_time",property = "collectTime"),
            @Result(column = "collect_time",property = "collectTime"),
            @Result(column = "image_urls",property = "imageUrls"),
            @Result(column = "writer",property = "writer"),
            @Result(column = "copyright",property = "copyright")
    })
    List<BookCollect> findCollectBook(@Param("userId") Long userId);

    /**
     *  添加收藏资讯
     * @param learnCollect
     */
    @Insert("insert into user_learn(user_id,learn_id,collect_time) values(#{userId},#{learnId},#{collectTime})")
    void insertCollectLearn(LearnCollect learnCollect);

    /**
     *  添加收藏书籍
     * @param bookCollect
     */
    @Insert("insert into user_book(user_id,book_id,collect_time) values(#{userId},#{bookId},#{collectTime})")
    void insertCollectBook(BookCollect bookCollect);

    /**
     * 删除收藏资讯
     * @param learnCollect
     */
    @Delete("delete from user_learn where user_id=#{userId} and learn_id=#{learnId}")
    void deleteCollectLearn(LearnCollect learnCollect);

    /**
     *  删除收藏书籍
     * @param bookCollect
     */
    @Delete("delete from user_book where user_id=#{userId} and book_id=#{bookId}")
    void deleteCollectBook(BookCollect bookCollect);
}
